package com.wenxixuanwu.fruitstore.controller;

import com.wenxixuanwu.fruitstore.entity.Cart;
import com.wenxixuanwu.fruitstore.entity.CartVO;
import com.wenxixuanwu.fruitstore.entity.User;
import com.wenxixuanwu.fruitstore.service.CartService;
import com.wenxixuanwu.fruitstore.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author :小凡凡
 * @version :1.0
 * @Project :fruitstore
 * @Date :2022/5/7
 */
@RestController
@RequestMapping("/cart")
public class CartController extends BaseController{

    @Autowired
    private CartService cartService;

    @GetMapping("/findCart")
    public List<CartVO> findCartByUid(HttpSession session){
        User user = (User) session.getAttribute("u");
        List<CartVO> data = cartService.findVoByUid(user.getId());
        return data;
    }

    @GetMapping("/add/num/{cid}")
    public Integer addNum(@PathVariable("cid") Integer cid){
        Integer number = cartService.addNum(cid);
        return number;
    }

    @GetMapping("/addCart/{id}")
    public Integer addCart(@PathVariable("id") Integer pid,HttpSession session){
        Integer integer = cartService.addCart(pid,session);
        return 1;
    }

    @GetMapping("/deleteCart/{cid}")
    public Integer deleteCart(@PathVariable("cid") Integer cid){
        Integer result = cartService.deleteCart(cid);
        return 1;
    }

    @PostMapping("/list")
    public Integer[] getVOByCid(@RequestBody List<CartVO> cartVOList,HttpSession session){
        List<Integer> listCid = new ArrayList<>();
        for(CartVO cartVO : cartVOList){
            listCid.add(cartVO.getCid());
        }
        Integer[] cids = listCid.toArray(new Integer[listCid.size()]);
        return cids;
    }

    @GetMapping("/getCartList")
    public JsonResult<List<CartVO>> getCart(Integer[] cid, HttpSession session){
        System.out.println(Arrays.toString(cid));
        List<CartVO> data = cartService.getVOByCid(cid, session);
        return new JsonResult<>(OK,data);
    }
}
